package cn.jdy.plugins.admin;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.jdy.common.constant.GlobalConstant;
import cn.jdy.common.ctrl.BaseCtrl;
import cn.jdy.framework.log.BaseLog;
import cn.jdy.framework.util.DateUtil;
import cn.jdy.framework.util.NginxPurgeCacheUtil;
import cn.jdy.platform.service.ConfService;
import cn.jdy.plugins.entity.HelpInfo;
import cn.jdy.plugins.service.HelpService;

/**
 * @title 帮助中心控制类
 * @date 2014-03-12
 */
@Controller
@RequestMapping("/help")
public class HelpCtrl extends BaseCtrl {

	@Autowired
	private HelpService helpService;
	@Autowired
	private ConfService confService;
	//默认列表路径
	private static String viewName = "redirect:/help/list?s=";

	/**
	 * 初始化
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView list(@RequestParam(required = false) Integer s) {
		ModelAndView mv = new ModelAndView();
		if(s!=null)
			switch(s) {
				case 1: mv.addObject("msg", "保存成功！");break;
				case 2: mv.addObject("msg", "更新成功！");break;
				case 3: mv.addObject("msg", "删除成功！");break;
				case 4: mv.addObject("msg", "操作失败！");break;
				default: break;
			}
		mv.setViewName("/operate/helpList.jsp");
		return mv;
	}

	/**
	 * 异步查询
	 * @param helpInfo
	 * @return
	 */
	@RequestMapping("/queryByPage")
	@ResponseBody
	public Map<String, Object> queryByPage(HelpInfo helpInfo) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try {
			jsonMap.put("dataList", helpService.query(helpInfo));
			jsonMap.put("iTotalDisplayRecords", helpService.count(helpInfo));
			jsonMap.put("iTotalRecords", helpService.count());
		} catch(Exception e) {
			BaseLog.e(this.getClass(), "查询帮助中心问题异常", e);
		}
		return jsonMap;
	}

	/**
	 * 新增
	 * @return
	 */
	@RequestMapping("/edit")
	public ModelAndView edit() {
		return edit(null);
	}

	/**
	 * 编辑
	 * @param id
	 * @return
	 */
	@RequestMapping("/edit/{id}")
	public ModelAndView edit(@PathVariable Integer id) {
		ModelAndView mv = new ModelAndView("/operate/helpDetail.jsp");
		mv.addObject("column", this.confService.queryByType(GlobalConstant.TYPE_HELP, null));
		mv.addObject("data", id==null||id==0 ? id : this.helpService.find(id));
		return mv;
	}

	/**
	 * 保存（更新）帮助中心
	 * @param helpInfo
	 * @return
	 */
	@RequestMapping("/save")
	public ModelAndView save(HelpInfo helpInfo) {
		ModelAndView mv = new ModelAndView();
		try {
			if(helpInfo.getId()==null) {
				helpInfo.setUserid(getUser().getId());
				helpInfo.setCreateTime(DateUtil.getCurrentDate(DateUtil._DEFAULT2));
				this.helpService.save(helpInfo);
				mv.setViewName(this.viewName.concat("1"));
			} else {
				this.helpService.update(helpInfo);
				mv.setViewName(this.viewName.concat("2"));
				// 清理内容页缓存
        NginxPurgeCacheUtil.purge(6, helpInfo.getId());
			}
			// 清理内容页缓存
      NginxPurgeCacheUtil.purge(5, helpInfo.getId());
		} catch(Exception e) {
			mv.setViewName(this.viewName.concat("4"));
			BaseLog.e(getClass(), "帮助中心问题保存异常！", e);
		}
		return mv;
	}
	
	/**
	 * 删除帮助中心
	 * @return
	 */
	@RequestMapping("/del/{id}")
	public ModelAndView del(@PathVariable Integer id) {
		ModelAndView mv = new ModelAndView();
		try {
			this.helpService.delete(id);
			mv.setViewName(this.viewName.concat("3"));
			// 清理内容页缓存
      NginxPurgeCacheUtil.purge(6, id);
		} catch(Exception e) {
			mv.setViewName(this.viewName.concat("4"));
			BaseLog.e(getClass(), "删除帮助中心异常！", e);
		}
		return mv;
	}

}
